<template>
  <!-- 回款 -->
  <el-dialog title="回款" :visible.sync="showHk" @close="cancelHk" width="50%" append-to-body>
    <el-form ref="hkform" :model="form" :rules="ruleshk" label-width="120px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="是否回款" prop="sfhk">
            <el-select v-model="form.sfhk" style="width: 100%;" placeholder="请选择">
              <el-option
                v-for="item in hkList"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="回款金额" prop="hkje">
            <el-input v-model="form.hkje" placeholder="请输入回款金额" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="回款日期" prop="hksj">
            <el-date-picker clearable
                            style="width: 100%"
                            v-model="form.hksj"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="请选择回款时间">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="buttonLoading" type="primary" @click="returnBtn">确 定</el-button>
      <el-button @click="cancelHk">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { updateWgd } from '@/api/gd/wgd'

export default {
  name: 'HkForm',
  props: {
    showHk: {
      type: Boolean
    },
    formInfo: {
      type: Object
    }
  },
  data() {
    return {
      form: {},
      ruleshk: {
        hkje: [
          { required: true, message: "回款金额不能为空", trigger: "blur" }
        ],
        hksj: [
          { required: true, message: "回款时间不能为空", trigger: "blur" }
        ],
        sfhk: [
          { required: true, message: "回款时间不能为空", trigger: "blur" }
        ],
      },
      buttonLoading: false,
      hkList: []
    }
  },
  created() {
    this.form = this.formInfo
    // 是否回款
    this.getDicts("gd_sfhk").then(res => {
      this.hkList = res.data
    })
  },
  methods: {
    change() {
      this.$forceUpdate()
    },
    cancelHk(){
      this.$emit('close')
    },
    returnBtn() {
      // 判断当前的开票状态，如果未开票
      this.$refs["hkform"].validate(valid => {
        if (valid) {
          // 如果是未开票状态则将状态改为已开票
          this.form.isLog = true
          updateWgd(this.form).then(response => {
            this.$modal.msgSuccess("回款成功！");
            this.cancelHk()
          }).finally(() => {
            this.buttonLoading = false;
          });
        }
      })
    },
  }
}
</script>

<style scoped>

</style>
